using ASP;
using IPMS.AccessAuth;
using System;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;


public partial class AddUsers : Page, IRequiresSessionState
{
     protected void AddUserToRole(string newUserName, string roleInformation)
    {
        Roles.AddUserToRole(newUserName, this.GroupName.SelectedValue.ToString());
        Auth runMethod = new Auth(null, newUserName, null);
        try
        {
            runMethod.UpdateUser(newUserName, Convert.ToInt16(this.txtAlertAdvDay.Text), this.chkSuperUser.Checked);
        }
        catch (Exception)
        {
        }
    }

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        if (!this.Page.User.IsInRole("Administrators"))
        {
            this.AddUserToRole(this.CreateUserWizard1.UserName, this.GetDefaultRoleForNewUser());
            this.CreateUserWizard1.ActiveStepIndex = this.CreateUserWizard1.WizardSteps.IndexOf(this.CreateUserWizard1.CompleteStep);
        }
    }

    private string GetDefaultRoleForNewUser()
    {
        if (ConfigurationManager.AppSettings["DefaultRoleForNewUser"] == null)
        {
            throw new Exception("DefaultRoleForNewUser was not been defined in the appsettings section of config");
        }
        string defaultRole = ConfigurationManager.AppSettings["DefaultRoleForNewUser"];
        if (string.IsNullOrEmpty(defaultRole))
        {
            throw new Exception("DefaultRoleForNewUser does not contain a default value");
        }
        if ((string.Compare(defaultRole, "4") >= 0) || (string.Compare(defaultRole, "0") < 0))
        {
            throw new ArgumentException("DefaultRoleForNewUser defined in the appsettings has to be between 0 and 2");
        }
        return ConfigurationManager.AppSettings["DefaultRoleForNewUser"];
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    private void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "UserList");
            if (!canView.CanAdd())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        if (!this.Page.IsPostBack && this.Page.User.IsInRole("Administrators"))
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.UserSettings.ToString() + " | " + ISitemap.AddUsers;
            this.CreateUserWizard1.LoginCreatedUser = false;
            this.GroupName.SelectedValue = this.GetDefaultRoleForNewUser();
        }
    }

    protected void Wizard_FinishButton_Click(object sender, WizardNavigationEventArgs e)
    {
        if (this.Page.User.IsInRole("Administrators"))
        {
            this.AddUserToRole(this.CreateUserWizard1.UserName, this.GroupName.SelectedValue);
        }
    }
}

